package 领扣算法.AA中等题.设计一个ATM机器;

class ATM {
//20 ，50 ，100 ，200 和 500
    long[] data=new long[5];
    int[] cost=new int[]{20,50,100,200,500};
    public ATM() {

    }

    public void deposit(int[] banknotesCount) {
        for (int i = 0; i < banknotesCount.length; i++) {
            data[i]+=banknotesCount[i];
        }
    }

    public int[] withdraw(int amount) {
        int res[] = new int[5];
        for (int i = 4; i >=0; i--) {
            if (data[i]>0){
                //尝试找钱
                res[i]=(int)Math.min(amount/cost[i],data[i]);
                data[i]-=res[i];
                amount-=res[i]*cost[i];
            }
        }
        if (amount!=0){
            //恢复
            for (int i = 0; i < data.length; i++) {
                data[i]+=res[i];
            }
            return new int[]{-1};
        }
        else
            return res;
    }
}
